package com.propertree.mailer.config;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import com.propertree.mailer.bean.EmailAttributes;
import com.propertree.mailer.bean.EmailAttributesConfiguration;

public class EmailAttributeHelper {

	public static EmailAttributes createEmailAttribute(
			EmailAttributesConfiguration config) {
		EmailAttributes emailAttribues = new EmailAttributes();
		emailAttribues.setSubject(readFile(config.getEmailSubjectFile()));
		emailAttribues.setBody(readFile(config.getEmailBodyFile()));
		return emailAttribues;
	}

	private static String readFile(String filePath) {
		FileReader fis = null;
		BufferedReader br = null;
		String text = "";
		try {
			fis = new FileReader(filePath);
			br = new BufferedReader(fis);

			String sCurrentLine;
			while ((sCurrentLine = br.readLine()) != null) {
				text += sCurrentLine;
			}
			return text;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return text;

	}
}
